package day10;

import java.util.LinkedList;
import java.util.Scanner;

public class BracketDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入括号:>");
        String line = sc.nextLine();
        if(match(line))
        System.out.println("匹配！");
        else
            System.out.println("不匹配！");
    }
    private static boolean match(String line){
    char[] arr = line.toCharArray();
        LinkedList<Character> list = new LinkedList<>();
        list.push(arr[0]);
        for(int i = 1;i<arr.length;i++){
            Character c = arr[i];
            if(list.isEmpty()){
                list.push(c);
                continue;
            }
            Character top = list.getFirst();
            if((top.equals('(') && c.equals(')')) || (top.equals('[') && c.equals(']')) || (top.equals('{') && c.equals('}'))){
                //7. 匹配成功
                //弹出栈顶元素
                list.pop();
            }else{
                //8. 匹配失败 - 继续压入栈
                list.push(arr[i]);
            }
        }
        //如果集合中没有元素 - 成功的
        return list.isEmpty();
    }
}